package com.ktgj.hotel.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.hotel.common.ElongUtils;
import com.ktgj.hotel.domain.HotelSupplierLink;
import com.ktgj.hotel.domain.base.HotelDetailInput;
import com.ktgj.hotel.domain.base.HotelDetailOutput;
import com.ktgj.hotel.domain.base.HotelDetailResponse;
import com.ktgj.hotel.domain.rooms.*;
import com.ktgj.hotel.mapper.HotelSupplierLinkMapper;
import com.ktgj.hotel.service.IHotelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.hotel.mapper.HotelInfoMapper;
import com.ktgj.hotel.domain.common.HotelInfo;
import com.ktgj.hotel.service.IHotelInfoService;

/**
 * 酒店详情Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-11-22
 */
@Service
public class HotelInfoServiceImpl implements IHotelInfoService 
{
    @Autowired
    private HotelInfoMapper hotelInfoMapper;

    @Autowired
    private IHotelService hotelService;

    @Autowired
    private HotelSupplierLinkMapper hotelSupplierLinkMapper;

    /**
     * 查询酒店详情
     * 
     * @param id 酒店详情ID
     * @return 酒店详情
     */
    @Override
    public HotelInfo selectHotelInfoById(Long id)
    {
        return hotelInfoMapper.selectHotelInfoById(id);
    }

    /**
     * 查询酒店详情列表
     * 
     * @param hotelInfo 酒店详情
     * @return 酒店详情
     */
    @Override
    public List<HotelInfo> selectHotelInfoList(HotelInfo hotelInfo)
    {
        return hotelInfoMapper.selectHotelInfoList(hotelInfo);
    }

    /**
     * 新增酒店详情
     * 
     * @param hotelInfo 酒店详情
     * @return 结果
     */
    @Override
    public int insertHotelInfo(HotelInfo hotelInfo)
    {
        hotelInfo.setCreateTime(DateUtils.getNowDate());
        return hotelInfoMapper.insertHotelInfo(hotelInfo);
    }

    /**
     * 修改酒店详情
     * 
     * @param hotelInfo 酒店详情
     * @return 结果
     */
    @Override
    public int updateHotelInfo(HotelInfo hotelInfo)
    {
        hotelInfo.setUpdateTime(DateUtils.getNowDate());
        return hotelInfoMapper.updateHotelInfo(hotelInfo);
    }

    /**
     * 批量删除酒店详情
     * 
     * @param ids 需要删除的酒店详情ID
     * @return 结果
     */
    @Override
    public int deleteHotelInfoByIds(Long[] ids)
    {
        return hotelInfoMapper.deleteHotelInfoByIds(ids);
    }

    /**
     * 删除酒店详情信息
     * 
     * @param id 酒店详情ID
     * @return 结果
     */
    @Override
    public int deleteHotelInfoById(Long id)
    {
        return hotelInfoMapper.deleteHotelInfoById(id);
    }

    /**
     * 酒店介绍
     * @param hotelId
     * @return
     */
    @Override
    public HotelHomeDetailInfo queryHotelDetailInfo(Long hotelId) {
        return hotelInfoMapper.queryHotelDetailInfo(hotelId);
    }

    public long millisecond(String date) throws Exception {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date));
        } catch (Exception e){
            throw new Exception("日期转换错误");
        }
        return calendar.getTimeInMillis();
    }

    /**
     * 设施服务
     * @param hotelId
     * @return
     */
    @Override
    public HotelHomeFacilityInfo queryHotelFacilityInfo(String hotelId) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (hotelId != null && !hotelId.equals("") && !hotelId.equals("0")){
            List<HotelSupplierLink> hotelSupplierLinkList = hotelSupplierLinkMapper.selectHotelSupplierLinkByHotelCode(hotelId);

            List<Long> count = new ArrayList<>();
            for (HotelSupplierLink hotelSupplierLink : hotelSupplierLinkList) {
                long millisecond;
                try {
                    millisecond = millisecond(format.format(hotelSupplierLink.getCreateTime()));
                } catch (Exception e){
                    throw new Exception(e.getMessage());
                }

                count.add(millisecond);
            }
            count.sort((x, y) -> Double.compare(y, x));

            if (hotelSupplierLinkList.get(0).getHotelId() != 0){
                HotelHomeFacilityInfo hotelHomeFacilityInfo = hotelInfoMapper.queryHotelFacilityInfo(Long.valueOf(hotelId));
                if (hotelHomeFacilityInfo != null && !hotelHomeFacilityInfo.equals("")){
                    return hotelHomeFacilityInfo;
                }
                else {
                    throw new Exception("未查询到设施服务信息");
                }
            }
            else {
                throw new Exception("未查到酒店对应id");
            }
        }
        else {
            throw new Exception("缺少必要参数");
        }
    }

    /**
     * 酒店房型页图片
     * @param request
     * @return
     */
    @Override
    public HotelGraphicResponse hotelGraphicIntroduction(HotelGraphicRequest request) throws Exception {
        HotelGraphicResponse response = new HotelGraphicResponse();
        HotelDetailInput hotelDetailInput = new HotelDetailInput();
        hotelDetailInput.ArrivalDate = request.ArrivalDate;
        hotelDetailInput.DepartureDate = request.DepartureDate;
        hotelDetailInput.HotelIds = request.HotelIds;
        HotelDetailOutput hotelDetailOutput;
        if (hotelDetailInput.HotelIds != null && !hotelDetailInput.HotelIds.equals("") &&
                hotelDetailInput.ArrivalDate != null && !hotelDetailInput.ArrivalDate.equals("") &&
                hotelDetailInput.DepartureDate != null && !hotelDetailInput.DepartureDate.equals("")){
            hotelDetailOutput = ElongUtils.queryByIdHotel(hotelDetailInput);
            if (!hotelDetailOutput.Code.equals("0")){
                throw new Exception("未查询到酒店信息");
            }
        }
        else {
            throw new Exception("缺少必要参数");
        }


        if (hotelDetailOutput.Code.equals("0")) {
            for (HotelDetailResponse.HotelsBean hotel : hotelDetailOutput.Result.getHotels()) {
                if (hotel.getImages().size() > 0) {
                    for (HotelDetailResponse.HotelsBean.Images image : hotel.getImages()) {
                        for (HotelDetailResponse.HotelsBean.Images.Locations location : image.getLocations()) {
                            ProductUrl productUrl = new ProductUrl();
                            productUrl.setPictureUrl(location.getUrl());
                            if (response.getProductUrls().size() > 9){
                                break;
                            }
                            response.getProductUrls().add(productUrl);
                        }
                    }
                } else {
                    for (HotelDetailResponse.HotelsBean.DetailBean detailBean : hotel.getDetail()) {
                        ProductUrl productUrl = new ProductUrl();
                        productUrl.setPictureUrl(detailBean.getThumbNailUrl());
                        if (response.getProductUrls().size() > 9){
                            break;
                        }
                        response.getProductUrls().add(productUrl);
                    }
                }
            }
        }
        return response;
    }
}
